package L612;

import java.util.Stack;

/**
 * @program: javase
 * @description: 有效的括号
 * @author: luolidong
 * @create: 2021-06-12 19:20
 * @version: 1.0
 */
public class Demo4 {
    public boolean isValid(String s) {
        Stack<Character> stack=new Stack<>();
        int i=0;
        while(i<s.length()){
            if(judge(s.charAt(i))!='0'){
                stack.push(s.charAt(i));
            }else{
                if(stack.isEmpty()) return false;
                if(judge(stack.peek())==s.charAt(i)){
                    stack.pop();
                }else{return false;}
            }
            i++;
        }
        return stack.isEmpty();
    }
    public char judge(char c){
        switch(c){
            case '{': return '}';
            case '(': return ')';
            case '[': return ']';
            default: return '0';
        }
    }


    public static void main(String[] args) {
        boolean valid = new Demo4().isValid("]{");
        System.out.println(valid);
    }
}
